

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * @author liuchun
 */
//为了让Panel 不停的重绘子弹，需要将MyPanel做成线程
public class Mypanel extends JPanel implements KeyListener,Runnable {
    MyTank myTank = null;
    //定义敌人的坦克
    Vector<EnemyTank> enemyTanks = new Vector<>();
    //定义一个Vector,存放炸弹
    Vector<Bomb> bombs = new Vector<>();
    Image image1 = null;
    Image image2 = null;
    Image image3 = null;
    int enemyTankSize = 3;
    public Mypanel(){
        myTank = new MyTank(500,500);
        myTank.setSpeed(2);

        for (int i = 0; i < enemyTankSize; i ++){
            //创建敌人坦克
           EnemyTank enemyTank =new EnemyTank((100*i+1),0);
           //设置方向
           enemyTank.setDirect(2);
           //启动敌人坦克
            new Thread(enemyTank).start();
           //给该Enemytanak加入一个子弹
            Shot shot =new Shot(enemyTank.getX()+20,enemyTank.getY()+60,enemyTank.getDirect());
            enemyTank.shots.add(shot);
            new Thread(shot).start();//启动线程

           //加入
           enemyTanks.add(enemyTank);
        }
        //初始化图片对象
        image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.gif"));
        image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif"));
        image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif"));
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 1000, 750);
        //画出自己的坦克
        if (myTank!= null && myTank.isLive) {
            drawTank(myTank.getX(), myTank.getY(), g, myTank.getDirect(), 1);
        }
        //画出子弹
            for (int i = 0; i < myTank.shots.size(); i++) {
                Shot shot = myTank.shots.get(i);
                if (shot != null && shot.isLive == true) {
                    //System.out.println("子弹被绘制!");
                    g.draw3DRect(shot.x, shot.y, 3, 3, false);
                }else {
                    myTank.shots.remove(shot);
                }
            }

        for (int i = 0; i < bombs.size(); i++) {
            Bomb bomb = bombs.get(i);//
            if(bomb.life > 6){
                g.drawImage(image1,bomb.x,bomb.y,60,60,this);
            }else if(bomb.life > 3){
                g.drawImage(image2,bomb.x,bomb.y,60,60,this);
            }else {
                g.drawImage(image3,bomb.x,bomb.y,60,60,this);
            }
            bomb.liveDown();
            if (bomb.life == 0){
                bombs.remove(bomb);
            }
        }
        //画出敌人的坦克
        for (int i = 0; i < enemyTanks.size(); i++) {
            //取出坦克
            EnemyTank enemyTank = enemyTanks.get(i);
            if (enemyTank.isLive) {
                drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirect(), 0);

                //画出敌人坦克的子弹
                for (int j = 0; j < enemyTank.shots.size(); j++) {
                    Shot shot = enemyTank.shots.get(j);
                    //绘制
                    if (shot.isLive) {
                        g.draw3DRect(shot.x, shot.y, 3, 3, false);
                    } else {
                        enemyTank.shots.remove(shot);
                    }
                }
            }
        }
    }

    /**
     * @param x
     * @param y
     * @param g 画笔
     * @param direct 方向
     * @param type 类型
     */
    //编写方法，绘制坦克
    public void drawTank(int x,int y,Graphics g,int direct,int type){
        //根据不同类型，绘制不同坦克
        switch (type){
            case 0:
                g.setColor(Color.cyan);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;
        }
        //根据不同方向，绘制坦克(0:上 1：右 2：下 3：左
        switch (direct){
            case 0://表示向上
                g.fill3DRect(x,y,10,60,false);//坦克左边的轮子
                g.fill3DRect(x+30,y,10,60,false);//坦克右边的轮子
                g.fill3DRect(x+10,y+10,20,40,false);
                g.drawOval(x+10,y+20,20,20);//圆形盖子
                g.drawLine(x+20,y+30,x+20,y);
                break;
            case 1://右
                g.fill3DRect(x,y,60,10,false);//坦克左边的轮子
                g.fill3DRect(x,y+30,60,10,false);//坦克右边的轮子
                g.fill3DRect(x+10,y+10,40,20,false);
                g.drawOval(x+20,y+10,20,20);//圆形盖子
                g.drawLine(x+30,y+20,x+60,y+20);//炮桶
                break;
            case 2://下
                g.fill3DRect(x,y,10,60,false);//坦克左边的轮子
                g.fill3DRect(x+30,y,10,60,false);//坦克右边的轮子
                g.fill3DRect(x+10,y+10,20,40,false);
                g.drawOval(x+10,y+20,20,20);//圆形盖子
                g.drawLine(x+20,y+30,x+20,y+60);
                break;
            case 3://左
                g.fill3DRect(x,y,60,10,false);//坦克左边的轮子
                g.fill3DRect(x,y+30,60,10,false);//坦克右边的轮子
                g.fill3DRect(x+10,y+10,40,20,false);
                g.drawOval(x+20,y+10,20,20);//圆形盖子
                g.drawLine(x+30,y+20,x,y+20);//炮桶
//            default:
//                System.out.println("暂时还没有处理!");
        }
    }
    public void hitEnemyTank(){
//        for (int i = 0; i < myTank.shots.size(); i++) {
//            Shot shot =myTank.shots.get(i);
//            if(shot!=null && shot.isLive) {//我的子弹还是存活的
//                for (int j = 0; i < enemyTanks.size(); j++) {
//                    EnemyTank enemyTank = enemyTanks.get(j);
//                    hitTank(myTank.shot, enemyTank);
//                }
//            }
            //判断是否击中
            if(myTank.shot!=null && myTank.shot.isLive){//我的子弹还是存活的
                for (int k = 0; k < enemyTanks.size() ; k++) {
                    EnemyTank enemyTank = enemyTanks.get(k);
                    hitTank(myTank.shot,enemyTank);
                }

            }
        }

    public void hitMytank(){
        //遍历所有坦克
        for (int i = 0; i <enemyTanks.size() ; i++) {
            EnemyTank enemyTank = enemyTanks.get(i);//取出
            //遍历enemyTank所有的子弹
         for(int j = 0 ;j < enemyTank.shots.size(); j++){
             Shot shot = enemyTank.shots.get(j);
             if (myTank.isLive && shot.isLive){
                 hitTank(shot,myTank);
             }
         }
        }
    }


    //编写方法，判断我方的子弹是否击中敌人坦克
    //什么时候判断是否击中敌方坦克？
    public void hitTank(Shot s,Tank enemyTank) {
        //判断击中坦克
        switch (enemyTank.getDirect()) {
            case 0:
            case 1:
            case 2:
                if (s.x > enemyTank.getX() && s.x < enemyTank.getX() + 40
                        && s.y > enemyTank.getY() && s.y < enemyTank.getY() + 60) {
                    s.isLive = false;
                    enemyTank.isLive = false;
                    enemyTanks.remove(enemyTank);
                    Bomb bomb = new Bomb(enemyTank.getX(),enemyTank.getY());
                    bombs.add(bomb);
                }
                break;
            case 3:
                if (s.x > enemyTank.getX() && s.x < enemyTank.getX() + 60
                        && s.y > enemyTank.getY() && s.y < enemyTank.getY() + 40) {
                    s.isLive = false;
                    enemyTank.isLive = false;
                }
                break;
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    //处理按键事件
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_W){
            myTank.setDirect(0);
            if (myTank.getY() > 0) {
                myTank.moveUp();
            }
            //修改坦克坐标 坦克坐标是私有的，怎么处理？--在tank类创建移动方法,直接对象调用
        }else if (e.getKeyCode() == KeyEvent.VK_S){
            myTank.setDirect(2);
            if (myTank.getY() +110 < 750) {
                myTank.moveDown();
            }
        }else if (e.getKeyCode() == KeyEvent.VK_A){
            myTank.setDirect(3);
            if (myTank.getX() > 0) {
                myTank.moveLeft();
            }
        }else if (e.getKeyCode() == KeyEvent.VK_D){
            myTank.setDirect(1);
            if (myTank.getX() +80 < 1000) {
                myTank.moveRight();
            }
        }
        if (e.getKeyCode() == KeyEvent.VK_J){
//            if (myTank.shot == null || !myTank.shot.isLive) {
//                myTank.shotEnemyTank();
//            }
            myTank.shotEnemyTank();
        }
        repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            hitEnemyTank();
            hitMytank();
            this.repaint();
        }
    }
}
